package com.ft.adbix.utils.data.gather.manager;

import android.content.Context;
import android.text.TextUtils;

import com.ft.adbix.oaid.util.OAIDUtils;
import com.ft.adbix.utils.AdLogUtils;
import com.ft.adbix.utils.data.SPManager;
import com.ft.adbix.utils.data.gather.util.ClassUtils;
import com.ft.adbix.oaid.OAIDManager;
import com.ft.adbix.utils.data.synchronize.constant.SpConstant;

public class DatagatherOaidManager {
    private static volatile DatagatherOaidManager instance;
    private boolean isImportOAIDAdapter;
    private static String OAID_ADAPTER_MANAGER_PATH = "com.ft.adbix.oaid.OAIDManager";


    public static DatagatherOaidManager getInstance() {
        if (instance == null) {
            synchronized (DatagatherOaidManager.class) {
                if (instance == null) {
                    instance = new DatagatherOaidManager();
                }
            }
        }
        return instance;
    }

    public DatagatherOaidManager() {
        if (ClassUtils.isLibraryCompile(OAID_ADAPTER_MANAGER_PATH)) {
            AdLogUtils.i("oaid 库已加载");
            isImportOAIDAdapter = true;
        }else{
            AdLogUtils.i("oaid 库未加载");
        }
    }

    /**
     * 初始化oaid适配器
     *
     * @param context
     * @param isCanUseOaid 是否支持使用OAID
     */
    public void init(Context context, OAIDUtils.OnOAIDLoadCallBack callBack,boolean isCanUseOaid) {
        if (!isCanUseOaid) {
            return;
        }
        if (isImportOAIDAdapter) {
            OAIDManager.getInstance().init(context, callBack, isCanUseOaid);
        }
    }

    public String getOAID() {
        if (!isImportOAIDAdapter) {
            return "";
        }
        String oaid = SPManager.getInstance().getString(SpConstant.SP_ADBIX_AD_OAID);
        if (TextUtils.isEmpty(oaid)) {
            oaid = OAIDManager.getInstance().getOAID();
            SPManager.getInstance().putString(SpConstant.SP_ADBIX_AD_OAID, oaid);
        }

        return oaid;
    }

    public String getAAID() {
        if (!isImportOAIDAdapter) {
            return "";
        }
        String aaid = SPManager.getInstance().getString(SpConstant.SP_ADBIX_AD_AAID);
        if (TextUtils.isEmpty(aaid)) {
            aaid = OAIDManager.getInstance().getAAID();
            SPManager.getInstance().putString(SpConstant.SP_ADBIX_AD_AAID, aaid);
        }
        return aaid;
    }

    public String getVAID() {
        if (!isImportOAIDAdapter) {
            return "";
        }
        String vaid = SPManager.getInstance().getString(SpConstant.SP_ADBIX_AD_VAID);
        if (TextUtils.isEmpty(vaid)) {
            vaid = OAIDManager.getInstance().getVAID();
            SPManager.getInstance().putString(SpConstant.SP_ADBIX_AD_VAID, vaid);
        }
        return vaid;
    }
}
